﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Dependency;
using RabbitMQ.GNClient;
using RabbitMQ.GNClient.Events;
using GN.Pay.Core.Repositories;
using GN.Pay.Core.Services;

namespace GN.Pay.Core.Tasks.Queue.Impl
{
    /// <summary>
    /// 付款申请过期消息队列
    /// </summary>
    class PayApplyExpireConsumerQueue : AConsumerQueue<string>, IPayApplyExpireConsumerQueue
    {
        public PayApplyExpireConsumerQueue(IIocManager iocManager)
            : base(QueueType.PayApplyExpire, iocManager)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        protected override ushort MaxPrefetchCount
        {
            get
            {
                return 10;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventConsumer"></param>
        /// <param name="data"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        protected override async Task Consumer(IEventingConsumer eventConsumer, string data, DeliverEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                eventConsumer.Channel.Ack(e.DeliveryTag);
                await Task.FromResult(0);
            }
            try
            {
                this.UpdateUnitOfWork(() =>
                {
                    var applyRepository = IocManager.Resolve<ITradePayApplyRepository>();
                    var apply = applyRepository.Get(data);
                    if (apply != null)
                    {
                        var items = apply.TradePayApplyChannels.ToList();
                        if (items.Count > 0)
                        {
                            var syn = IocManager.Resolve<ITradeSynchronization>();
                            foreach (var item in items)
                            {
                                try
                                {
                                    syn.TradeChannelPay(item.Id);
                                }
                                catch (Exception err)
                                {
                                    this.Logger.Error("支付过期同步出错:" + err.Message, err);
                                }
                            }
                        }
                        else
                        {
                            if (apply.IsExpire())
                            {
                                applyRepository.Delete(apply);
                            }
                        }
                    }
                });
                await Task.FromResult(0);
            }
            catch (Exception err)
            {
                this.Logger.Error("支付过期同步出错:" + err.Message, err);
                this.DelayConsumer(eventConsumer, data, e, new TimeSpan(0, 0, 10));
                await Task.FromResult(0);
            }
            finally
            {
                eventConsumer.Channel.Ack(e.DeliveryTag);
            }

        }
    }
}
